<?php


namespace app\index\controller;
use app\index\model\File as FileModel;
use app\index\model\Notice as NoticeModel;
use http\Header;
use think\Controller;
use think\Db;
use think\Request;

class File extends Controller
{
    //课程列表
    public function  fileList()
    {
        $this -> view -> assign('title', '课程列表');
        $this -> view -> assign('keywords', '软件工程专业教学管理系统');
        $this -> view -> assign('desc', '软件工程专业教学管理系统');
        $this -> view -> count = FileModel::count();
        $list = FileModel::paginate(1);
        $this -> view -> assign('list', $list);
        //渲染文件列表模板
        return $this -> view -> fetch('file_list');
    }


    public function uploadFile(){
        return $this->fetch();
    }

    //添加操作的界面
    public function  fileAdd()
    {
        $this->view->assign('title','添加课程表');
        $this->view->assign('keywords','软件工程专业教学管理系统');
        $this->view->assign('desc','软件工程专业教学管理系统');
        return $this->view->fetch('file_add');
    }

    //添加操作
    public function write(){
        $class = request()->param('class');
        $times = request()->param('times');
        $file = request()->file('image');
        // 移动到框架应用根目录/public/upload/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'upload');
        }
        $filepath=$info->getSaveName();//将图片的地址存进数据库
        $date=['class'=>$class,'times'=>$times,'filepath'=>$filepath];
        if(Db::table('file')->insert($date)){
            return $this->success("添加成功");
        }else{
            return $this->success("添加失败");
        }
        return $this->fetch();
    }

    public function show(){
        $info=Db::table("file")->select();
        $this->assign("info",$info);
        return $this->fetch();
    }

    //渲染编辑课表界面
    public function fileEdit(Request $request)
    {
        $file_id = $request -> param('id');
        $result = FileModel::get($file_id);
        $this->view->assign('title','编辑通知');
        $this->view->assign('keywords','软件工程专业教学管理系统');
        $this->view->assign('desc','软件工程专业教学管理系统');
        $this->view->assign('file_info',$result->getData());
        return $this->view->fetch('file_edit');
    }

    //更新数据操作
    public function editFile(Request $request)
    {
        $data = $request -> param();

        //设置更新条件
        $condition = ['id'=>$data['id']];

        //更新当前记录
        $result = FileModel::update($data,$condition);

        //设置返回数据
        $status = 0;
        $message = '更新失败,请检查';

        //检测更新结果,将结果返回给file_edit模板中的ajax提交回调处理
        if (true == $result) {
            $status = 1;
            $message = '恭喜, 更新成功~~';
        }
        return ['status'=>$status, 'message'=>$message];
    }

    //文件下载
    public function download(Request $request)
    {
        $id = input('id');
        $file = Db::table('file')->find($id);
        //从数据库下载文件名
        $file_name=$file['filepath'];
        //下载文件存放目录
        $file_dir=ROOT_PATH . 'public' . DS . 'upload' . "/";
        echo $file_dir . $file_name;
        //检查文件是否存在
        if(!file_exists($file_dir . $file_name)){
            echo "文件找不到";
            exit();
        }else{
            //打开文件
            $file1 = fopen($file_dir . $file_name,"r");
            //输入文件标签
            Header("Content-type: application/octet-stream");
            Header("Accept-Ranges: bytes");
            Header("Accept-Length: " . filesize($file_dir . $file_name));
            Header("Content-Disposition: attachment; filename=" . $file_name);
            ob_clean();
            flush();    //可清楚文件中多余的路径问题以及解决乱码问题

            //输出文件内容
            //读取文件内容并直接输出到浏览器
            echo fread($file1, filesize($file_dir . $file_name));
            fclose($file1);
            exit();
        }
    }

}